Birmingham | 26-ITP-May | Ogbemi Mene | Sprint 2 | coursework #1439
Birmingham | 26-ITP-May | Ogbemi Mene | Sprint 2 | coursework #1439meneogbemi42-bit wants to merge 18 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
|
|
||
| } |
There was a problem hiding this comment.
Really good explanation of scope and variable declarations here, just wanted to highlight the extra if statement you added. Are you sure it handles the empty string case like you describe?
There was a problem hiding this comment.
this is the best explanation i could come up with.
By checking typeof str !== "string", you are asking: "If the input is anything other than a string, stop processing and just return the input exactly as it is." If you send a string: It skips the if block and proceeds to do the work.
If you send a number: The if condition is true, so it returns the number and ignores the rest of the function.
There was a problem hiding this comment.
So your understanding of what the if statement does (as you've explained here) is good, but that's not what you've said in the comments 🙂 You said:
the if condition i added is to ensure that if an empty string is passed to the function, it will return the empty string instead of throwing an error.
But as you have described, the if condition checks for the data type, not whether the string is empty. How would you check for that?
There was a problem hiding this comment.
yes, the initial statement was not correct, i looked at it critically a saw that this if statement will trow an error, because i did not add this ( | | ) operator, Because of the | | operator, if the string is empty, the function returns str. to handle it i will add || str.length === 0) to the if condition. and this will handle it.
There was a problem hiding this comment.
Please add this second condition then 🙂
| // =============> the explanation: | ||
| // The function sum has a return statement before the addition operation, which causes the function to return undefined immediately. | ||
| // To fix this, the return statement should be placed after the addition operation. | ||
| // because the function is not a string i have to remove the backticks and use a normal string concatenation to print the result correctly. |
There was a problem hiding this comment.
// because the function is not a string i have to remove the backticks and use a normal string concatenation to print the result correctly.
What happens if you tried to use the backticks here with a function that returns a number?
There was a problem hiding this comment.
If you use backticks with a function that returns a number, JavaScript will automatically convert that number into a string so it can be combined with the rest of your text. just wanted to indicate that i was not working with string functions
There was a problem hiding this comment.
Right, I'm just trying to highlight why you changed
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);To be
console.log(sum(10, 32));These are the only backticks in this file that you removed. Why do that?
There was a problem hiding this comment.
When I am writing code , I often switch to the shorter version to keep the code block as clean as possible, but your original approach (using backticks) is actually better practice for real-world debugging because it makes the console output much easier to read and understand.
There was a problem hiding this comment.
I understand - either console.log is fine! They both serve the same purpose. To bring it back to my original comment, the thing you wrote:
because the function is not a string i have to remove the backticks and use a normal string concatenation to print the result correctly.
Doesn't really make sense here. The function doesn't contain backticks or string concatenation; the only backticks you removed you were in the console.log which doesn't have any bearing on the function itself or how it works. You clearly understand the function's issue and how to solve! I just think this comment serves to only confuse things and doesn't really add anything. You can remove it!
|
Really nice explanations Ogbemi! Just a few comments but good work overall. |
Moved the return statement to after the addition operation to ensure the function returns the correct result.
This PR contains debugging, implementation, and interpretation work across multiple coding exercises focusing on function parameters, return values, and string manipulation in JavaScript.